package by.dvrudenko.avtobaza.webapp.page;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import by.dvrudenko.avtobaza.datamodel.Responses;
import by.dvrudenko.avtobaza.datamodel.User;
import by.dvrudenko.avtobaza.datamodel.enums.UserRole;
import by.dvrudenko.avtobaza.services.ResponsesService;
import by.dvrudenko.avtobaza.webapp.app.BasicAuthenticationSession;
import by.dvrudenko.avtobaza.webapp.page.contacts.ContactsPage;
import by.dvrudenko.avtobaza.webapp.page.createResponses.ResponsesPage;
import by.dvrudenko.avtobaza.webapp.page.createResponses.panel.ResponsesDetailsPanel;
import by.dvrudenko.avtobaza.webapp.page.createUser.CreateUserPage;
import by.dvrudenko.avtobaza.webapp.page.home.HomePage;
import by.dvrudenko.avtobaza.webapp.page.lang.LanguageSelectionPanel;
import by.dvrudenko.avtobaza.webapp.page.login.component.LoginLogoutPanel;



public abstract class BaseLayout extends WebPage implements Serializable {
	
	@Inject
	private ResponsesService responsesService;
	
	final protected User user = BasicAuthenticationSession.get().getUser();
	

	@Override
	protected void onInitialize() {
		super.onInitialize();

		add(new Label("headerTitle", getPageTitle()));
		
		FeedbackPanel feedBack = new FeedbackPanel("feedbackpanel");
		feedBack.setOutputMarkupId(true);
		add(feedBack);
		
		add(new LoginLogoutPanel("login-logout-panel"));
		
		add(new LanguageSelectionPanel("lang-select"));
		
		add(new Link<Void>("goToHomePage") {
			@Override
			public void onClick() {			
				setResponsePage(HomePage.class);
			};
		});
		
		/*add(new Link<Void>("contacts") {
			@Override
			public void onClick() {
				setResponsePage(ContactsPage.class);
			};
		});*/
		
		add(new Link<Void>("responses") {
			@Override
			public void onClick() {
				setResponsePage(ResponsesPage.class);
			};
		});
		
		// visible only for non - registered user
		add(new Link<Void>("register") {
			
			@Override
			protected void onConfigure() {
				super.onConfigure();
				setVisible(!BasicAuthenticationSession.get().isSignedIn());
			}
			
			@Override
			public void onClick() {
				User user = new User();
				setResponsePage(new CreateUserPage(user, UserRole.USER));
			};
		});
		
		add(new Label("userName", new Model((user != null ? user.getName() : "")+" " +
				 (user != null ? user.getRole().name() : ""))));

		final List<Responses> allFiveResponses = responsesService.getRandomResponses(5);
		
		add(new ListView<Responses>("responsesDetailsPanel", allFiveResponses) {
			@Override
			protected void populateItem(ListItem<Responses> item) {
				Responses responses = item.getModelObject();
				boolean isLast = allFiveResponses.size() - 1 == item.getIndex();
				item.add((new ResponsesDetailsPanel("responses-panel", responses, isLast)));
			}
		});
	}
	
	

	protected IModel<String> getPageTitle() {
		return new Model<String>(getClass().getSimpleName());
	}
	
	protected Timestamp convertDateToTimestamp(TextField<Date> yearDate){
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date date1 = null;
		try {
			date1 = dateFormat.parse(yearDate.getInput().toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long time = date1.getTime();
		return new Timestamp(time);
	}

	
}
